<!DOCTYPE HTML>
<link rel="help" href="http://url.spec.whatwg.org/#dom-url-username">
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<script>

test(function() {
    var url = new URL('http://www.domain.com/');
    assert_equals(url.username, '');

    url = new URL('http://user@www.domain.com/');
    assert_equals(url.username, 'user');

    url.username = 'changed';
    assert_equals(url.toString(), 'http://changed@www.domain.com/');

    url.username = '';
    assert_equals(url.toString(), 'http://www.domain.com/');
}, 'username property');

test(function() {
    var url = new URL('about:test');
    assert_equals(url.username, '');

    url.username = 'user';
    assert_equals(url.username, '');
    assert_equals(url.toString(), 'about:test');
}, 'username property on non relative URL');

test(function() {
    var url = new URL('http://user:@www.domain.com/');
    var expected = 'user';
    assert_equals(url.username, expected);

    assert_throws_js(TypeError, () => { url.href = 'invalid'; });
    assert_equals(url.username, expected);

    url.username = 'changed';
    assert_equals(url.username, 'changed');
    assert_equals(url.href, 'http://changed@www.domain.com/');
}, 'username property invalid URL');

</script>
